package com.oracle.lingshangejia.activity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Message;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.oracle.lingshangejia.Constant;
import com.oracle.lingshangejia.R;
import com.oracle.lingshangejia.adapter.NoSovledAdapter;
import com.oracle.lingshangejia.base.BaseActivity;
import com.oracle.lingshangejia.bean.SeekMedicineBean;
import com.oracle.lingshangejia.utils.OkHttpUtils;
import com.oracle.lingshangejia.utils.SpUtils;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshListener;

import net.lucode.hackware.magicindicator.MagicIndicator;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

/**
 * 寻医问药S
 */
public class SeekMedicineActivity extends BaseActivity implements View.OnClickListener {
    private TextView tv_title;
    private ImageButton ib_back;
    private MagicIndicator seekmedicine_magic_indicator;
    private ViewPager seekmedicine_view_pager;
    private  String[] CHANNELS = new String[]{"待处理问题", "已处理任务","删除问题"};
    private List<String> mDataList = Arrays.asList(CHANNELS);
    private ArrayList<Fragment> fragments;
    private RecyclerView seek_recycleview;
    private NoSovledAdapter adapter;
    private SmartRefreshLayout mRefreshLayout;//刷新
    @Override
    public int intiLayout() {
        return R.layout.activity_seekmedicine;
    }

    @Override
    public void initView() {

        tv_title=findViewById(R.id.tv_title);
        tv_title.setText("寻医问药");
        ib_back=findViewById(R.id.ib_back);
        ib_back.setOnClickListener(this);
        seekmedicine_magic_indicator=findViewById(R.id.seekmedicine_magic_indicator);
        seekmedicine_view_pager=findViewById(R.id.seekmedicine_view_pager);
        seek_recycleview=findViewById(R.id.seek_recycleview);
        LinearLayoutManager linearLayoutManager=new LinearLayoutManager(this);
        linearLayoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        seek_recycleview.setLayoutManager(linearLayoutManager);
        mRefreshLayout = findViewById(R.id.srl);
        showLoading();

    }

    private void initRefresh(){
        mRefreshLayout.setOnRefreshListener(new OnRefreshListener() {
            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                getNetSeekMedicine();
            }
        });
      /*  mRefreshLayout.setOnLoadmoreListener(new OnLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                mRefreshLayout.finishLoadmore();
            }
        });*/
    }


    @Override
    public void initData() {
        getNetSeekMedicine();
        initRefresh();
//        fragments=new ArrayList<Fragment>();
//        NoSolvedFragment noSolvedFragment=new NoSolvedFragment();
//        SolvedFragment solvedFragment=new SolvedFragment();
//        DelQuestionFragment delQuestionFragment=new DelQuestionFragment();
//        fragments.add(noSolvedFragment);
//        fragments.add(solvedFragment);
//        fragments.add(delQuestionFragment);
//        FieledMissionsFragmentPagerAdapter mExamplePagerAdapter = new FieledMissionsFragmentPagerAdapter(getSupportFragmentManager(),fragments);
//        seekmedicine_view_pager.setAdapter(mExamplePagerAdapter);
//        initMagicIndicator();
    }
//    private void initMagicIndicator() {
//        CommonNavigator commonNavigator = new CommonNavigator(this);
//        commonNavigator.setAdjustMode(true);
//        commonNavigator.setAdapter(new CommonNavigatorAdapter() {
//
//            @Override
//            public int getCount() {
//                return 3;
//            }
//
//            @Override
//            public IPagerTitleView getTitleView(Context context, final int index) {
//                SimplePagerTitleView simplePagerTitleView = new ColorTransitionPagerTitleView(context);
//                simplePagerTitleView.setNormalColor(getResources().getColor(R.color.colorBlack));
//                simplePagerTitleView.setSelectedColor(getResources().getColor(R.color.colorBlue));
//                simplePagerTitleView.setText(mDataList.get(index));
//                simplePagerTitleView.setOnClickListener(new View.OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        seekmedicine_view_pager.setCurrentItem(index);
//                    }
//                });
//                return simplePagerTitleView;
//            }
//
//            @Override
//            public IPagerIndicator getIndicator(Context context) {
//                LinePagerIndicator linePagerIndicator = new LinePagerIndicator(context);
//                linePagerIndicator.setMode(LinePagerIndicator.MODE_WRAP_CONTENT);
//                linePagerIndicator.setColors(getResources().getColor(R.color.colorBlue));
//                return linePagerIndicator;
//            }
//        });
//        seekmedicine_magic_indicator.setNavigator(commonNavigator);
//        ViewPagerHelper.bind(seekmedicine_magic_indicator, seekmedicine_view_pager);
//    }
    private void getNetSeekMedicine(){
        int userid= (int) SpUtils.get(SeekMedicineActivity.this,"ysid",0);
        HashMap<Object,Object> map = new HashMap<>();
        map.put("userId",userid);
        OkHttpUtils.getNetWork(this, Constant.baseurl+"online/xywy/fqwtList",map,handler);
    }
    @SuppressLint("HandlerLeak")
    private android.os.Handler handler=new android.os.Handler(){
        @Override
        public void handleMessage(Message msg) {
            switch (msg.what){
                case 1:
                    dismissLoading();
                    try{
                        String s= (String) msg.obj;
                        Gson gson=new Gson();
                        SeekMedicineBean bean=gson.fromJson(s,SeekMedicineBean.class);
                        if(bean.getCode()==0){
                            adapter=new NoSovledAdapter(SeekMedicineActivity.this,bean.getData());
                            seek_recycleview.setAdapter(adapter);
                            adapter.setOnItemClickListener(new NoSovledAdapter.OnItemClickListener() {
                                @Override
                                public void onItemClick(View view, int whyId) {
                                    Intent intent=new Intent(SeekMedicineActivity.this,FieledMissionsDetailsActivity.class);
                                    intent.putExtra("xywyId",bean.getData().get(whyId).getId());
                                    startActivity(intent);
                                }
                            });
                            mRefreshLayout.finishRefresh();
                        }else{
                            Toast.makeText(SeekMedicineActivity.this,bean.getMsg(),Toast.LENGTH_SHORT).show();
                        }
                    }catch (Exception e){
                        e.printStackTrace();
                    }

                    break;
                case 2: //连接上服务器，错误
                    String error= (String) msg.obj;
                    Toast.makeText(SeekMedicineActivity.this,error,Toast.LENGTH_SHORT).show();
                    break;
                case 3:  //没有网络
                    Toast.makeText(SeekMedicineActivity.this,"网络不可用",Toast.LENGTH_SHORT).show();
                    break;
                case 4: //没有连接上服务器
                    Toast.makeText(SeekMedicineActivity.this,"连接服务器失败",Toast.LENGTH_SHORT).show();
                    break;
            }
        }
    };
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.ib_back:
                finish();
                break;
        }
    }
}
